<template>
  <div>
    <el-dialog title="收货地址" :visible.sync="is.show" :before-close="cancel">
      <el-form :model="specsForm" ref="specsForm" :rules="rules">
        <!-- 名称 -->
        <el-form-item
          label="规格名称"
          prop="specsname"
          :label-width="formLabelWidth"
        >
          <el-input v-model="specsForm.specsname" autocomplete="off"></el-input>
        </el-form-item>

        <!-- 规格属性 -->
        <el-form-item
          label="规格属性"
          prop="attrs"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="text"
            class="attr"
            clearable
            autocomplete="off"
          ></el-input>
          <el-button type="primary" @click="addAttrs">新增规格属性</el-button>

          <div class="newattr" v-for="(item, index) in attrArr" :key="index">
            <el-input
              v-model="item.val"
              class="attr"
              autocomplete="off"
            ></el-input>
            <el-button type="danger" @click="delAttr(index)">移除</el-button>
          </div>
        </el-form-item>

        <!-- 状态 -->
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="specsForm.status"
            active-color="#13ce66"
            inactive-color="#ccc"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="enter">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>


<script>
//引入辅助性函数
import { mapState, mapActions } from "vuex";
//调用添加 修改接口
import { addOreditSpecs, specsinfo } from "../../request/api";
export default {
  data() {
    //自定义验证规则
    const valAttrs = (a, b, callback) => {
      if (this.attrArr.length == 0 && this.text == "") {
        callback(new Error("请输入规格属性"));
      } else {
        callback();
      }
    };
    return {
      specsForm: {
        specsname: "",
        attrs: "",
        status: 1,
      },
      text: "",
      attrArr: [], //新增规格属性的数组
      formLabelWidth: "120px",
      //表单规则验证
      rules: {
        specsname: {
          required: true,
          message: "请输入规格名称",
          trigger: "blur",
        },
        attrs: { validator: valAttrs, trigger: "blur" },
      },
    };
  },

  //计算属性 用来获取vuex中的数据 辅助性函数mapState
  computed: {
    ...mapState("specs", ["specsList", "is"]),
  },

  //方法 辅助性函数mapActions 和其他方法
  methods: {
    ...mapActions("specs", ["getSpecs", "getTotal"]),

    //点击取消事件
    cancel() {
      //关闭弹窗
      this.$store.commit("specs/changeDialog", false);
      //清空表格
      this.specsForm = {
        specsname: "",
        attrs: "",
        status: 1,
      };
      //清除表单验证规则
      this.$refs.specsForm.resetFields();
      //清除输入框
      this.text = "";
      //清除规格属性的数组
      this.attrArr = [];
    },

    //用来回显编辑数据
    dataForm(id) {
      //调取接口
      specsinfo({ id }).then((res) => {
        if (res.code == 200) {
          //赋值
          this.specsForm = res.list[0];

          this.attrArr = res.list[0].attrs.map((val) =>{return {val:val}});

          this.specsForm.id = id;
        } else {
          //失败提示
          this.$message.error(res.msg);
        }
      });
    },

    //点击确认事件
    enter() {
      //是添加 或者 编辑
      let url = this.specsForm.id ? "specsedit" : "specsadd";
      //确认事件
      this.$refs.specsForm.validate((val) => {
        if (val) {
          //把数组转换成需要的字符串
          this.specsForm.attrs = this.attrArr.map((val) => val.val).join(",");
          //调用接口
          addOreditSpecs(url, this.specsForm).then((res) => {
            if (res.code == 200) {
              //关闭弹窗
              this.cancel();
              //成功弹窗
              this.$message.success(res.msg);
              //重新调用接口
              this.getSpecs();
            } else {
              //失败原因
              this.$message.error(res.msg);
            }
          });
        }
      });
    },

    //点击新增属性按钮
    addAttrs() {
      if (this.text == "") {
        this.$message.error("请输入规格属性");
        return;
      }
      if (this.attrArr.length > 4) {
        this.$message.error("属性最多设置五条");
        return;
      }

      //增加到attrs  数组里
      this.attrArr.push({ val: this.text });
      //清空输入框
      this.text = "";
    },

    //点击移除按钮事件
    delAttr(index) {
      this.attrArr.splice(index, 1);
    },
  },
};
</script>


<style lang='less' scoped>
.attr {
  width: 60%;
  float: left;
  margin-right: 20px;
}
.newattr {
  margin-top: 10px;
}
</style>